package com.example.book.view

import android.os.Bundle
import androidx.fragment.app.Fragment
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.TextView
import androidx.core.text.isDigitsOnly
import androidx.databinding.DataBindingUtil
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.viewModels
import androidx.lifecycle.viewModelScope
import com.example.book.model.Book
import com.example.book.repository.BookRepository
import com.example.book.viewmodel.BookViewModel
import com.example.demo.R
import com.example.demo.databinding.FragmentBookBinding
import dagger.hilt.android.AndroidEntryPoint
import kotlinx.coroutines.launch
import javax.inject.Inject

// the fragment initialization parameters
private const val ISBN = "isbn"

@AndroidEntryPoint
class BookFragment @Inject constructor(bookRepository: BookRepository): Fragment() {

    var isbn: String? = null

    private val viewModel: BookViewModel by viewModels {
        BookViewModel.Factory(bookRepository)
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            isbn = it.getString(ISBN)
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        // Inflate the layout for this fragment
        //return inflater.inflate(R.layout.fragment_book, container, false)

        val binding: FragmentBookBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_book,container,false)
        binding.lifecycleOwner = this
        binding.bookViewModel = viewModel
        return  binding.root
    }

//    companion object {
//        /**
//         * Use this factory method to create a new instance of
//         * this fragment using the provided parameters.
//         *
//         * @param isbn the ISBN of the book
//         * @return A new instance of fragment BookFragment.
//         */
//        @JvmStatic
//        fun newInstance(isbn: String) =
//            BookFragment().apply {
//                arguments = Bundle().apply {
//                    putString(ISBN, isbn)
//                }
//            }
//    }
}